package com.lanou.one;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

/**
 * 1) 创建一个List，在List 中增加三个工人，基本信息如下：
 * 姓名 年龄 工资
 * zhang3 18 3000
 * li4 25 3500
 * wang5 22 3200
 * 2) 在li4 之前插入一个工人，信息为：姓名：zhao6，年龄：24，工资3300
 * 3) 删除wang5 的信息
 * 4) 利用for 循环遍历，打印List 中所有工人的信息
 * 5) 利用迭代遍历，对List 中所有的工人调用work 方法。
 *
 * @author zyj
 * @date 2022年05月24日 9:04
 */
public class Test {
    public static void main(String[] args) {
        List<Work> list = new ArrayList<>();
        list.add(new Work("wang5", 22, 3200));
        list.add(new Work("zhang3", 18, 3000));
        list.add(new Work("li4", 25, 3500));
        int index = getIndex(list);
        list.add(index, new Work("zhao6", 24, 3300));


        //删除元素
//        for (int i = 0; i < list.size(); i++) {
//            Work work = list.get(i);
//            if ("wang5".equals(work.getName())){
//                list.remove(work);
//            }
//        }

        //不能通过foreach遍历去删除元素
//        for (Work w:list) {
//            if ("wang5".equals(w.getName())){
//                list.remove(w);
//            }
//        }

        Iterator<Work> iterator = list.iterator();
        while (iterator.hasNext()) {
            Work work = iterator.next();
            if ("wang5".equals(work.getName())) {
                iterator.remove();
            }
        }

        list.forEach((work) ->

        {
            System.out.println(work);
        });

    }

    private static int getIndex(List<Work> list) {
        for (int i = 0; i < list.size(); i++) {
            Work work = list.get(i);
            if ("li4".equals(work.getName())) {
                return i;
            }
        }

        return -1;
    }
}

class Work {
    //姓名 年龄 工资
    private String name;
    private int age;
    private int salary;

    public Work() {
    }

    public Work(String name, int age, int salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Work work = (Work) o;
        return age == work.age && salary == work.salary && Objects.equals(name, work.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, salary);
    }

    @Override
    public String toString() {
        return "Work{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }
}
